package com.ruiysoft.crm.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ruiysoft.crm.dto.CustomerEx;
import com.ruiysoft.crm.entity.Customer;
import com.ruiysoft.crm.entity.SelectLocation;
import com.ruiysoft.crm.mapper.SelectLocationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2018-08-13
 */
@RestController
@RequestMapping("selectLocation")
public class SelectLocationController {
    @Autowired
    SelectLocationMapper selectLocationMapper;

    @PostMapping("create")
    public Object create(@RequestBody SelectLocation selectLocation) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        System.out.println("输入: " + selectLocation.toString());

        try {
            selectLocation.setCreateTime(new Date());

            if (selectLocationMapper.insert(selectLocation) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        System.out.println("输出: " + map.toString());

        return map;
    }

    @GetMapping("delete")
    public Object delete(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            if (selectLocationMapper.deleteById(id) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("edit")
    public Object edit(@RequestBody SelectLocation selectLocation) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            if (selectLocationMapper.updateById(selectLocation) > 0)
                map.put("success", true);
            else
                map.put("errorMsg", "更新失败!");

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("query")
    @GetMapping("query")
    public Object query() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            EntityWrapper<SelectLocation> entityWrapper = new EntityWrapper();
            entityWrapper.orderBy("createTime", false);
            List<SelectLocation> list = selectLocationMapper.selectPage(new Page<Customer>(1, 25), entityWrapper);
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }
}
